package itbour.onetouchshow.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import itbour.onetouchshow.R;
import itbour.onetouchshow.bean.HomeDetailslBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.SpUtils;
import itbour.onetouchshow.utils.sharedpreference.PreferencesHelper;

/**
 * Created by guoh on 2018/6/26.
 * 功能描述：修改分享链接属性
 * 需要的参数：
 */
@SuppressLint("ValidFragment")
public class ChangeShareLinkStyleDialogFragment extends DialogFragment {


    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.et_title)
    EditText etTitle;
    @BindView(R.id.et_description)
    EditText etDescription;
    Unbinder unbinder;
    DialogType mAppType;
    private onDismissListener onDismissListener;

    @SuppressLint("ValidFragment")
    public ChangeShareLinkStyleDialogFragment(DialogType appType) {
        this.mAppType = appType;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().setCanceledOnTouchOutside(false);
        View view = inflater.inflate(R.layout.fr_change_link_style, null);
        unbinder = ButterKnife.bind(this, view);

        initView();
        return view;
    }

    private void initView() {
        String shareTitle = null;
        String shareContent = null;
        if (mAppType.equals(DialogType.TYPEOFCUSTOMAPP)) {
            shareTitle = PreferencesHelper.getData(AppConst.KEY_APP_SHARE_TITLE);
            shareContent = PreferencesHelper.getData(AppConst.KEY_APP_SHARE_CONTENT);
        } else if (mAppType.equals(DialogType.TYPEOFCUSTOMVIDEO)) {
            shareTitle = PreferencesHelper.getData(AppConst.KEY_CUSTOM_SHARE_TITLE);
            shareContent = PreferencesHelper.getData(AppConst.KEY_CUSTOM_SHARE_CONTENT);
        }
        HomeDetailslBean basicInfo = new Gson().fromJson(SpUtils.getInstance().getString(AppConst.APPBASEINFO), HomeDetailslBean.class);

        if (mAppType.equals(DialogType.TYPEOFCUSTOMAPP)) {
            if (TextUtils.isEmpty(shareTitle)) {
                shareTitle = basicInfo.getAppControl().getVideoShareTitle();
            }
            if (TextUtils.isEmpty(shareContent)) {
                shareContent = basicInfo.getAppControl().getVideoShareMsg();
            }
        } else if (mAppType.equals(DialogType.TYPEOFCUSTOMVIDEO)) {
            if (TextUtils.isEmpty(shareTitle)) {
                shareTitle = basicInfo.getAppControl().getCustomVideoShareTitle();
            }
            if (TextUtils.isEmpty(shareContent)) {
                shareContent = basicInfo.getAppControl().getCustomVideoShareMsg();
            }
        }
        etTitle.setText(shareTitle);
        etDescription.setText(shareContent);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = etTitle.getText().toString();
                String description = etDescription.getText().toString();

                if (mAppType.equals(DialogType.TYPEOFCUSTOMAPP)) {
                    if (!TextUtils.isEmpty(title)) {
                        PreferencesHelper.saveData(AppConst.KEY_APP_SHARE_TITLE, title);
                    } else {
                        PreferencesHelper.remove(AppConst.KEY_APP_SHARE_TITLE);
                    }
                    if (!TextUtils.isEmpty(description)) {
                        PreferencesHelper.saveData(AppConst.KEY_APP_SHARE_CONTENT, description);
                    } else {
                        PreferencesHelper.remove(AppConst.KEY_APP_SHARE_CONTENT);
                    }
                } else if (mAppType.equals(DialogType.TYPEOFCUSTOMVIDEO)) {
                    if (!TextUtils.isEmpty(title)) {
                        PreferencesHelper.saveData(AppConst.KEY_CUSTOM_SHARE_TITLE, title);
                    } else {
                        PreferencesHelper.remove(AppConst.KEY_CUSTOM_SHARE_TITLE);
                    }
                    if (!TextUtils.isEmpty(description)) {
                        PreferencesHelper.saveData(AppConst.KEY_CUSTOM_SHARE_CONTENT, description);
                    } else {
                        PreferencesHelper.remove(AppConst.KEY_CUSTOM_SHARE_CONTENT);
                    }
                }

                dismiss();
                if (onDismissListener != null) {
                    onDismissListener.onDismiss();
                }
            }
        });
    }

    public void onShow(Activity activity) {
        if (!isAdded()) {
            show(activity.getFragmentManager().beginTransaction(), "ChangeShareLinkStyleDialogFragment");
        }
    }

    public void setOnDismissListener(ChangeShareLinkStyleDialogFragment.onDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        //底部动画
        window.getAttributes().windowAnimations = R.style.shareDialogStyle;
        //设置背景透明
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public enum DialogType {
        TYPEOFCUSTOMAPP,
        TYPEOFCUSTOMVIDEO
    }

    public interface onDismissListener {
        void onDismiss();
    }
}
